package com.lzw.breezeNovel.webApp.service.impl.video;

import com.lzw.breezeNovel.model.entity.pojo.video.TvSeries;
import com.lzw.breezeNovel.model.entity.query.PaginationResultQuery;
import com.lzw.breezeNovel.model.enums.viedo.TvStatus;
import com.lzw.breezeNovel.webApp.mapper.video.TvSeriesMapper;
import com.lzw.breezeNovel.webApp.service.video.TvSeriesService;
import com.lzw.breezeNovel.webApp.vo.video.TvSeriesVo;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.service.impl.video
 * @date 2025/3/18
 * @description
 */
@Service
@SuppressWarnings("all")
public class TvSeriesServiceImpl implements TvSeriesService {

    /**
     * 注入mapper
     */
    @Autowired
    private TvSeriesMapper tvSeriesMapper;

    /**
     * 分页查询电视剧
     * @param pageNo
     * @param size
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public PaginationResultQuery<TvSeriesVo> getTvSeries(Integer pageNo, Integer size) {
        //计算分页
        int startPage = startPage(pageNo, size);
        //获取总数据
        Long count = tvSeriesMapper.selectCount();
        //判断是否有数据
        if (count == 0) {
            return new PaginationResultQuery<>(0, size, pageNo, 0, Collections.emptyList());
        }
        //获取数据
        List<TvSeries> tvSeries = tvSeriesMapper.selectListByPage(startPage, size);
        //转换数据
        List<TvSeriesVo> tvSeriesVos = Copy(tvSeries);
        //返回数据
        return new PaginationResultQuery<>(Math.toIntExact(count), size, pageNo, tvSeries.size(), tvSeriesVos);
    }

    /**
     * 获取相关电视剧
     *
     * @param s
     * @param type
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public List<TvSeriesVo> getRelatedTvSeries(String type, Integer videoId) {
        //判断类型是否为空
        if (type == null) {
            return List.of();
        }
        //获取数据
        List<TvSeries> tvSeriesList = tvSeriesMapper.selectRelatedTvSeries(type,videoId );
        if (tvSeriesList != null && !tvSeriesList.isEmpty()) {
            return Copy(tvSeriesList);
        }
        return List.of();
    }


    /**
     * 转换数据
     * @param tvSeries
     * @return
     */
    @NotNull
    private List<TvSeriesVo> Copy(List<TvSeries> tvSeries) {
        //判断数据是否为空
        if (tvSeries == null || tvSeries.isEmpty()) {
            return Collections.emptyList();
        }
        //拷贝数据
        return tvSeries.stream().map(TvSerie ->{
            TvSeriesVo tvSeriesVo = new TvSeriesVo();
            //进行对象拷贝
            BeanUtils.copyProperties(TvSerie,tvSeriesVo);
            //获取状态
            Integer status = TvSerie.getStatus();
            //判断状态
            if(status == TvStatus.SERIALIZE.getCode())
            {
                tvSeriesVo.setStatus(TvStatus.SERIALIZE.getDesc());
            }else
            {
                tvSeriesVo.setStatus(TvStatus.FINISH.getDesc());
            }
            return tvSeriesVo;
        }).toList();

    }

    /**
     * 计算分页
     * @param pageNo
     * @param size
     * @return
     */
    public int startPage(Integer pageNo, Integer size){
        //判断分页数据是否为空
        pageNo = Optional.of(pageNo).orElse(1);
        size = Optional.of(size).orElse(10);
        pageNo = Math.max(pageNo, 1);
        size = Math.max(size, 1);
        //计算分页
        pageNo = (pageNo - 1) * size;
        return pageNo;
    }
}
